package com.fm.app03.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fm.app03.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DaDiShuActivity extends AppCompatActivity {

    private List<ImageView> mImageViewList = new ArrayList<>();
    private ImageView mCurrentImageView;
    private TextView mScoreTextView;
    private Timer mTimer;
    private int mScore;
    private CountDownTimer mCountDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 隐藏状态栏和导航栏
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_IMMERSIVE);

        setContentView(R.layout.activity_da_di_shu);

        // 获取到最外层的 LinearLayout
        LinearLayout rootLayout = findViewById(R.id.dadisuAll);
        mScoreTextView = findViewById(R.id.fenshu);

        // 为每个 ImageView 设置监听器
        setImageViewClickListener(rootLayout);

        // 设置开始按钮的点击事件
        Button startButton = findViewById(R.id.start_button);
        startButton.setOnClickListener(v -> startGame());

        // 设置停止按钮的点击事件
        Button stopButton = findViewById(R.id.stop_button);
        stopButton.setOnClickListener(v -> stopGame());
    }

    private void setImageViewClickListener(View view) {
        if (view instanceof ImageView) {
            ImageView imageView = (ImageView) view;
            // 初始化所有的 ImageView，将其 tag 设置为 false
            imageView.setTag(false);
            mImageViewList.add(imageView);
            imageView.setOnClickListener(v -> {
                // 处理 ImageView 的点击事件
                boolean isHit = (boolean) v.getTag();
                if (isHit) {
                    mScore++;
                    mScoreTextView.setText("得分：" + mScore);
                }
            });
        } else if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View child = viewGroup.getChildAt(i);
                setImageViewClickListener(child);
            }
        }
    }

    private void startGame() {
        if(mCurrentImageView!=null){
            mCurrentImageView.setImageResource(R.drawable.dishu_1);
            mCurrentImageView.setTag(false);
        }
        // 更新当前 ImageView，并将其 tag 设置为 false
        mCurrentImageView = randomImg();
        // 先停止之前的定时器任务
        stopGame();

        // 初始化得分
        mScore = 0;
        mScoreTextView.setText("得分：" + mScore);

        // 开始每隔一秒更新地鼠出现状态
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(() -> {
                    mCurrentImageView.setImageResource(R.drawable.dishu_1);
                    mCurrentImageView.setTag(false);
                    mCurrentImageView = randomImg();
                    mCurrentImageView.setImageResource(R.drawable.dishu_2);
                    mCurrentImageView.setTag(true);
                });
            }
        };
        mTimer = new Timer();
        mTimer.schedule(task, 0, 1000);
        startCountdown();
    }


    private void stopGame() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }

        // 取消倒计时任务
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }

        if(mCurrentImageView!=null){
            mCurrentImageView.setTag(false);
        }

    }

    private void startCountdown() {
        TextView timerView = findViewById(R.id.my_timer);
        mCountDownTimer = new CountDownTimer(30000, 1000) { // 30 秒倒计时，每秒更新一次
            public void onTick(long millisUntilFinished) {
                timerView.setText("倒计时：" + millisUntilFinished / 1000);
            }
            public void onFinish() {
                timerView.setText("时间到！");
                stopGame();
            }
        }.start();
    }

    private ImageView randomImg() {
        // 创建一个 Random 对象
        Random rand = new Random();
        // 获取 mImageViewList 中的一个随机元素
        return mImageViewList.get(rand.nextInt(mImageViewList.size()));
    }
}